/**
 *Copyright (c) 2024 watereyes
 * safetymanagement is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan
 * PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */
package com.koron.scene.controller;

import com.koron.common.Excel;
import com.koron.common.StaffAttribute;
import com.koron.common.bean.StaffBean;
import com.koron.common.permission.SPIAccountAnno;
import com.koron.scene.domain.ProjectStaff;
import com.koron.scene.service.api.ProjectStaffService;
import com.koron.scene.utils.BusinessCode;
import com.koron.system.annotation.Log;
import com.koron.system.discrete.BusinessManipulation;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.swan.bean.MessageBean;
import springfox.documentation.annotations.ApiIgnore;

import java.io.File;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "ProjectStaff", description = "相关方人员")
@RestController
@RequestMapping("/projectStaff")
public class ProjectStaffController {

    private final static Logger logger = LoggerFactory.getLogger(ProjectStaffController.class);
    @Autowired
    private ProjectStaffService projectStaffService;

    @ApiOperation("项目相关方-相关方人员导入")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "projectId", value = "项目id", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "enterpriseId", value = "相关方id", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "file", value = "上传的excel文件", required = true, paramType = "query", dataType = "file")
    })
    @RequestMapping(value = "/importExcel.htm", method = {RequestMethod.POST})
    @Log(title = "相关方人员导入",businessModule = "工程管理-人员档案", businessManipulation = BusinessManipulation.INSERT)
    public MessageBean<?> importFromExcel(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user,
                                          @RequestBody MultipartFile file, String projectId, String enterpriseId) {

        if (user == null) {
            MessageBean<?> msg = new MessageBean();
            msg.setCode(BusinessCode.NO_LOGIN.getCode());
            msg.setDescription(BusinessCode.NO_LOGIN.getDescription());
            return msg;
        }
        return projectStaffService.importExcel(user, file, projectId, enterpriseId);
    }


    @RequestMapping(value = "/update.htm", method = RequestMethod.POST)
    @ApiOperation(value = "项目相关方-修改人员信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "staffId", value = "项目id", dataType = "String"),
            @ApiImplicitParam(name = "name", value = "名称", dataType = "String"),
            @ApiImplicitParam(name = "phoneNumber", value = "联系号码", dataType = "String"),
            @ApiImplicitParam(name = "joinDateStr", value = "加入时间", dataType = "String"),
            @ApiImplicitParam(name = "exitDateStr", value = "退出时间", dataType = "String"),

    })
    @Log(title = "修改人员信息",businessModule = "工程管理-人员档案", businessManipulation = BusinessManipulation.UPDATE)
    public MessageBean update(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user,
                              String staffId, String name, String phoneNumber, String joinDateStr, String exitDateStr) {
        if (user == null) {
            MessageBean<?> msg = new MessageBean();
            msg.setCode(BusinessCode.NO_LOGIN.getCode());
            msg.setDescription(BusinessCode.NO_LOGIN.getDescription());
            return msg;
        }
        return projectStaffService.update(user, staffId, name, phoneNumber, joinDateStr, exitDateStr);
    }

    @RequestMapping(value = "/list.htm", method = RequestMethod.GET)
    @ApiOperation(value = "项目相关方-项目人员列表信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "projectId", value = "项目id", dataType = "String"),
            @ApiImplicitParam(name = "enterpriseId", value = "相关方id", dataType = "String"),
            @ApiImplicitParam(name = "historyFlag", value = "是否历史:0.否,1是", dataType = "String"),
            @ApiImplicitParam(name = "name", value = "姓名", dataType = "String"),
            @ApiImplicitParam(name = "page", value = "当前页", dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "Integer")

    })
    public MessageBean<?> list(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user,
                               String projectId, String enterpriseId, Integer historyFlag, String name,
                               Integer page, Integer pageSize) {
        if (user == null) {
            MessageBean<?> msg = new MessageBean();
            msg.setCode(BusinessCode.NO_LOGIN.getCode());
            msg.setDescription(BusinessCode.NO_LOGIN.getDescription());
            return msg;
        }
        return projectStaffService.list(user, projectId, enterpriseId, historyFlag, name, page, pageSize);
    }


    @RequestMapping(value = "/managementList.htm", method = RequestMethod.GET)
    @ApiOperation(value = "人员档案-人员档案列表信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "员工姓名", dataType = "String"),
            @ApiImplicitParam(name = "enterpriseName", value = "相关方名称", dataType = "String"),
            @ApiImplicitParam(name = "projectName", value = "项目名称", dataType = "String"),
            @ApiImplicitParam(name = "page", value = "当前页", dataType = "Integer"),
            @ApiImplicitParam(name = "keyword", value = "关键词", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "Integer"),
            @ApiImplicitParam(name = "corpCode", value = "单位code", dataType = "String")
    })
    public MessageBean<?> managementList(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user,
                                         String name, String enterpriseName, String projectName, String keyword,
                                         Integer page, Integer pageSize, String corpCode) {
        if (user == null) {
            MessageBean<?> msg = new MessageBean();
            msg.setCode(BusinessCode.NO_LOGIN.getCode());
            msg.setDescription(BusinessCode.NO_LOGIN.getDescription());
            return msg;
        }
        return projectStaffService.managementList(user, name, enterpriseName, projectName, page, pageSize, corpCode,keyword);
    }

    @ApiOperation("项目相关方-下载模板")
    @RequestMapping(value = "/exportExcelTemplate.htm", method = {RequestMethod.GET})
    public HttpEntity<?> exportExcelTemplate(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user) {
        if (null == user) {
            return null;
        }
        Map<String, Object> map = new HashMap<>();
        File targetFile = new File("static/excel/ProjectStaff.xlsx");
        try {
            InputStream initialStream = getClass().getClassLoader().getResourceAsStream("static/excel/ProjectStaff.xlsx");
            FileUtils.copyInputStreamToFile(initialStream, targetFile);
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
        }
        return Excel.createFile("相关方人员导入模板", targetFile, "default.btl", map);
    }


    @RequestMapping(value = "/oneStaffProject.htm", method = RequestMethod.GET)
    @ApiOperation(value = "人员档案-人员项目信息(包含证照,培训)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "idCardNumber", value = "身份证", dataType = "String"),
            @ApiImplicitParam(name = "corpCode", value = "单位code", dataType = "String"),
            @ApiImplicitParam(name = "projectStaffId", value = "人员档案id", dataType = "String"),
    })
    public MessageBean<?> oneStaffProject(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user,
                                           String idCardNumber, String corpCode,
                                          String projectStaffId) {
        if (user == null) {
            MessageBean<?> msg = new MessageBean();
            msg.setCode(BusinessCode.NO_LOGIN.getCode());
            msg.setDescription(BusinessCode.NO_LOGIN.getDescription());
            return msg;
        }
        return projectStaffService.oneStaffProject(user, idCardNumber, corpCode, projectStaffId);
    }

    @RequestMapping(value = "/staffProjectList.htm", method = RequestMethod.GET)
    @ApiOperation(value = "人员档案-人员项目信息(包含证照,培训)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "idCardNumber", value = "身份证", dataType = "String"),
            @ApiImplicitParam(name = "corpCode", value = "单位code", dataType = "String"),
            @ApiImplicitParam(name = "page", value = "当前页", dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "Integer"),
    })
    public MessageBean<?> staffProjectList(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user,
                                           String idCardNumber, String corpCode,
                                           Integer page, Integer pageSize) {
        if (user == null) {
            MessageBean<?> msg = new MessageBean();
            msg.setCode(BusinessCode.NO_LOGIN.getCode());
            msg.setDescription(BusinessCode.NO_LOGIN.getDescription());
            return msg;
        }
        return projectStaffService.staffProjectList(user, idCardNumber, corpCode, page, pageSize);
    }

    @ApiOperation("相关方人员档案列表导出接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "员工姓名", dataType = "String"),
            @ApiImplicitParam(name = "enterpriseName", value = "相关方名称", dataType = "String"),
            @ApiImplicitParam(name = "projectName", value = "项目名称", dataType = "String"),
            @ApiImplicitParam(name = "page", value = "当前页", dataType = "Integer"),
    })
    @RequestMapping(value = "/exportList.htm", method = RequestMethod.GET)
    public HttpEntity<?> export(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user,
                                String name, String enterpriseName, String projectName,
                                String corpCode) {
        if (null == user) {
            return null;
        }
        List<ProjectStaff> list = projectStaffService.exportList(user, corpCode, name,enterpriseName,projectName);
        Map<String, Object> map = new HashMap<>();
        map.put("data", list);
        File targetFile = new File("static/excel/projectStaffList.xlsx");
        try {
            InputStream in = getClass().getClassLoader().getResourceAsStream("static/excel/projectStaffList.xlsx");
            FileUtils.copyInputStreamToFile(in, targetFile);
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
        }
        return Excel.createFile("人员档案列表", targetFile, "projectStaffList.btl", map);
    }
}
